package com.meitianhui.convenitenbuy.module.mine.presenter;

import com.google.gson.Gson;
import com.meitianhui.convenitenbuy.base.BaseResponse;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.http.ResponseObserver;
import com.meitianhui.convenitenbuy.module.mine.contract.RecommenderContract;
import com.meitianhui.convenitenbuy.module.mine.http.MineApiDataSource;
import com.meitianhui.convenitenbuy.module.mine.model.rxbusBean.RecommenderEventBean;
import com.meitianhui.convenitenbuy.module.mine.ui.activity.RecommenderActivity;
import com.meitianhui.convenitenbuy.module.order.model.bean.EmptyInfo;
import com.meitianhui.convenitenbuy.util.RxBus;
import com.meitianhui.convenitenbuy.util.TipUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by amy on 2017/11/16.
 */

public class RecommenderPresenter implements RecommenderContract.Presenter {

    private RecommenderActivity mContext;
    private RecommenderContract.View mView;

    public RecommenderPresenter(RecommenderActivity activity, RecommenderActivity context) {
        this.mView = activity;
        this.mContext = context;
    }

    @Override
    public void attachView(Object view) {
    }

    @Override
    public void detachView() {
    }

    @Override
    public void upLoadRecommender(String recommenderNumber) {
        Map<String, String> hashmap = new HashMap<>();
        hashmap.put("member_id", InfoProvider.getUserToken(mContext).getMember_id());
        hashmap.put("member_mobile", InfoProvider.getUserToken(mContext).getMobile());
        hashmap.put("recommend_mobile", recommenderNumber);

        MineApiDataSource.getInstance()
                .bindRecommender(InfoProvider.getUserToken(mContext).getUser_token(),
                        "consumer.consumer.userRecommendCreate", new Gson().toJson(hashmap))
                .subscribe(new ResponseObserver<BaseResponse<EmptyInfo>>(mContext) {
                    @Override
                    public void onSuccess(BaseResponse<EmptyInfo> s) {
                        TipUtils.showToast(mContext, "添加成功", 3);
                        mView.bindRecommenderSuccess(recommenderNumber);
                        // 通知首页显示推荐人号码
                        RxBus.get().post(new RecommenderEventBean(recommenderNumber));
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        TipUtils.showToast(mContext, msg, 3);
                    }
                });
    }

}
